/**
 * 
 */
package server;

import java.util.Iterator;
import java.util.Queue;

import controllers.MailSenderController;
import entities.Email;

/**
 * @author shlomi
 *
 */
public class ServerMailBot extends Thread {

	private final int minuteInMilliSec = 60000;
	
	
	private ServerHandler srvhnd;
	private Queue<Email> emails_list;
	
	public ServerMailBot(ServerHandler srvhnd) {
		this.srvhnd = srvhnd;
	}
	
	public void run(){
		emails_list = srvhnd.getEmails_list();
		
		while(true){
			Iterator<Email> iter = emails_list.iterator();
		
			while(iter.hasNext()) {
				// System.out.println("Sending...\n");
				Email eml = (Email)iter.next();
				MailSenderController.sendMail(eml);
				iter.remove();
				
			}
			
			// put thread to sleep for 30 seconds
			try {
				sleep(minuteInMilliSec/20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
